﻿using Bll;
using Entity.BBC;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Analysis.Controllers
{
    public class PurchaEditController : Controller
    {
        public static IList<DataDiction> proTypeList;
        public static IList<DataDiction> varietiList;
        public static IList<DataRela> dRelaList;

        // GET: PurchaEdit
        public ActionResult Index()
        {
            // 判断登录
            //if (Session["User"] == null)
            //{
            //    return RedirectToAction("Index", "LoginManager");
            //}
            String sqlStr = "SELECT * FROM DataDiction WHERE TypeDic IN(1,2,3,4,5)";
            IList<DataDiction> DataDictionList = BllBBCCommon.SelectEntityBySql<DataDiction>("DataDiction", sqlStr);
            IList<DataDiction> DDictionList1 = DataDictionList.Where(x => x.TypeDic == 1).ToList();
            IList<DataDiction> DDictionList2 = DataDictionList.Where(x => x.TypeDic == 2).ToList();
            IList<DataDiction> DDictionList3 = DataDictionList.Where(x => x.TypeDic == 3).ToList();
            IList<DataDiction> DDictionList4 = DataDictionList.Where(x => x.TypeDic == 4).ToList();
            IList<DataDiction> DDictionList5 = DataDictionList.Where(x => x.TypeDic == 5).ToList();
            ViewData["DDictionList1"] = DDictionList1;
            ViewData["DDictionList2"] = DDictionList2;
            ViewData["DDictionList3"] = DDictionList3;
            ViewData["DDictionList4"] = DDictionList4;
            ViewData["DDictionList5"] = DDictionList5;

            sqlStr = "SELECT * FROM DataRela";
            IList<DataRela> DataRelaList = BllBBCCommon.SelectEntityBySql<DataRela>("DataRela", sqlStr);
            ViewData["DataRelaList"] = DataRelaList;

            proTypeList = DDictionList2;
            varietiList = DDictionList5;
            dRelaList = DataRelaList;
            return View();
        }

        public ActionResult UpdPru(long PurID)
        {
            String sqlStr = "SELECT * FROM DataDiction WHERE TypeDic IN(1,2,3,4,5)";
            IList<DataDiction> DataDictionList = BllBBCCommon.SelectEntityBySql<DataDiction>("DataDiction", sqlStr);
            IList<DataDiction> DDictionList1 = DataDictionList.Where(x => x.TypeDic == 1).ToList();
            IList<DataDiction> DDictionList2 = DataDictionList.Where(x => x.TypeDic == 2).ToList();
            IList<DataDiction> DDictionList3 = DataDictionList.Where(x => x.TypeDic == 3).ToList();
            IList<DataDiction> DDictionList4 = DataDictionList.Where(x => x.TypeDic == 4).ToList();
            IList<DataDiction> DDictionList5 = DataDictionList.Where(x => x.TypeDic == 5).ToList();
            ViewData["DDictionList1"] = DDictionList1;
            ViewData["DDictionList2"] = DDictionList2;
            ViewData["DDictionList3"] = DDictionList3;
            ViewData["DDictionList4"] = DDictionList4;
            ViewData["DDictionList5"] = DDictionList5;

            sqlStr = "SELECT * FROM DataRela";
            IList<DataRela> DataRelaList = BllBBCCommon.SelectEntityBySql<DataRela>("DataRela", sqlStr);
            ViewData["DataRelaList"] = DataRelaList;
            proTypeList = DDictionList2;
            varietiList = DDictionList5;
            dRelaList = DataRelaList;

            PruData pruData = BllBBCCommon.SelectEntity<PruData>("PruData", PurID);
            ViewData["ddlPruData"] = pruData;

            return View();
        }

        /// <summary>
        /// 关联过滤产地
        /// </summary>
        /// <param name="DDID">类别ID</param>
        /// <returns></returns>
        public ActionResult ChangeProType(long DDID)
        {
            IList<DataRela> DataRelaList = dRelaList;
            IList<DataDiction> ProAddressList = proTypeList;

            DataRelaList = DataRelaList.Where(x => x.DDID1 == DDID).ToList();
            ProAddressList = ProAddressList.Where(x => DataRelaList.Select(y => y.DDID2).ToList().Contains(x.Id)).ToList();

            return Json(ProAddressList, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 关联过滤产品
        /// </summary>
        /// <param name="DDID1">类别ID</param>
        /// <param name="DDID2">产地</param>
        /// <returns></returns>
        public ActionResult ChangeProAddress(long DDID1, long DDID2)
        {
            IList<DataRela> DataRelaList1 = dRelaList;
            IList<DataRela> DataRelaList2 = dRelaList;
            IList<DataDiction> ProAddressList = varietiList;

            DataRelaList1 = DataRelaList1.Where(x => x.DDID1 == DDID1).ToList();
            DataRelaList2 = DataRelaList2.Where(x => x.DDID1 == DDID2).ToList();

            ProAddressList = ProAddressList.Where(x => DataRelaList1.Select(y => y.DDID2).ToList().Contains(x.Id) && DataRelaList2.Select(t => t.DDID2).ToList().Contains(x.Id)).ToList();
            return Json(ProAddressList, JsonRequestBehavior.AllowGet);
        }



        /// <summary>
        /// 增加采购数据
        /// </summary>
        /// <param name="proType">商品类别</param>
        /// <param name="proAddress">产地</param>
        /// <param name="purPrice">采购价格</param>
        /// <param name="shelfTime">上架时间</param>
        /// <param name="proCycle">商品周期</param>
        /// <param name="downTime">预计下架时间</param>
        /// <param name="spec">采购规格</param>
        /// <param name="palate">口感卖点</param>
        /// <param name="grade">果品等级</param>
        /// <param name="purNum">建议采购量</param>
        /// <param name="varieti">品种</param>
        /// <param name="fruitType">果型</param>
        /// <param name="retailPrice">建议零售价</param>
        /// <param name="knowled">产品特点知识</param>
        /// <returns></returns>
        public ActionResult AddPruData(string proType, string proAddress,string purPrice,string shelfTime,string proCycle,string downTime,string spec,string palate,
            string grade,string purNum,string varieti,string fruitType,string retailPrice,string knowled)
        {
            try
            {
                PruData tabPruData = new PruData();
                tabPruData.ProType = int.Parse(proType);
                tabPruData.ProAddress = int.Parse(proAddress);
                tabPruData.PurPrice = decimal.Parse(purPrice);
                tabPruData.ShelfTime = DateTime.Parse(shelfTime);
                tabPruData.ProCycle = proCycle;
                tabPruData.DownTime = DateTime.Parse(downTime);
                tabPruData.Spec = int.Parse(spec);
                tabPruData.Palate = palate;
                tabPruData.Grade = int.Parse(grade);
                if (!purNum.Equals(""))
                {
                    tabPruData.PurNum = int.Parse(purNum);
                }
                tabPruData.Varieti = varieti;
                tabPruData.FruitType = fruitType;
                if (!retailPrice.Equals(""))
                {
                    tabPruData.RetailPrice = decimal.Parse(retailPrice);
                }
                tabPruData.Knowled = knowled;

                int count = BllBBCCommon.AddNewEntity<PruData>("PruData", tabPruData);
                return (count > 0 ? Content("1") : Content("-1"));
            }
            catch (Exception ex)
            {
                return Content("-1");
            }
        }

        /// <summary>
        /// 修改采购数据
        /// </summary>
        /// <param name="PurID">ID</param>
        /// <param name="proType">商品类别</param>
        /// <param name="proAddress">产地</param>
        /// <param name="purPrice">采购价格</param>
        /// <param name="shelfTime">上架时间</param>
        /// <param name="proCycle">商品周期</param>
        /// <param name="downTime">预计下架时间</param>
        /// <param name="spec">采购规格</param>
        /// <param name="palate">口感卖点</param>
        /// <param name="grade">果品等级</param>
        /// <param name="purNum">建议采购量</param>
        /// <param name="varieti">品种</param>
        /// <param name="fruitType">果型</param>
        /// <param name="retailPrice">建议零售价</param>
        /// <param name="knowled">产品特点知识</param>
        /// <returns></returns>
        public ActionResult UpdPruData(string PurID, string proType, string proAddress, string purPrice, string shelfTime, string proCycle, string downTime,
            string spec, string palate, string grade, string purNum, string varieti, string fruitType, string retailPrice, string knowled)
        {
            try
            {
                PruData tabPruData = new PruData();
                tabPruData.Id = long.Parse(PurID);
                tabPruData.ProType = int.Parse(proType);
                tabPruData.ProAddress = int.Parse(proAddress);
                tabPruData.PurPrice = decimal.Parse(purPrice);
                tabPruData.ShelfTime = DateTime.Parse(shelfTime);
                tabPruData.ProCycle = proCycle;
                tabPruData.DownTime = DateTime.Parse(downTime);
                tabPruData.Spec = int.Parse(spec);
                tabPruData.Palate = palate;
                tabPruData.Grade = int.Parse(grade);
                if (!purNum.Equals(""))
                {
                    tabPruData.PurNum = int.Parse(purNum);
                }
                tabPruData.Varieti = varieti;
                tabPruData.FruitType = fruitType;
                if (!retailPrice.Equals(""))
                {
                    tabPruData.RetailPrice = decimal.Parse(retailPrice);
                }
                tabPruData.Knowled = knowled;

                int count = BllBBCCommon.UpdateEntity<PruData>("PruData", tabPruData);
                return (count > 0 ? Content("1") : Content("-1"));
            }
            catch (Exception ex)
            {
                return Content("-1");
            }
        }
    }
}